home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10727 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.6 KB  |  100 lines

  1. Path: news.bellglobal.com!stupy
  2. From: stupy@freenet.durham.org (Steve Tupy)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: Template functions with constants instead of types????
  5. Date: 6 Mar 1996 07:57:14 GMT
  6. Organization: Durham Free-net
  7. Message-ID: <4hjggq$k40@news.bellglobal.com>
  8. References: <313D005B.1E1D@interlinx.qc.ca>
  9. NNTP-Posting-Host: freenet.durham.org
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Mathieu Frenette (midk@interlinx.qc.ca) wrote:
  13. : Ok, now, let's go with the rough stuff! :)  What I want is to be able to 
  14. : define a function using a DEFINEd constant (let's name it "CONSTANT") 
  15. : and then be able to call the function with CONSTANT having arbitrary 
  16. : values.  For each value of CONSTANT, the compiler would generate a 
  17. : different function.
  18.  
  19. : In example, let's suppose we could define something like :
  20.  
  21. :   template <constant T> inline void Func( void )
  22. :   {
  23. :     printf( "%d\n", T );
  24. :   }
  25.  
  26.     No, no, you are missing a serious point of templating. It is used in
  27. cases where generic data types are used. Here you will run into problems
  28. when you instantiate a template using doubles or longs as you insist on
  29. using an integer type for your printf. You should use cout instead of printf
  30. so that all data types may be supported. Also, you do not use "constant" in
  31. a template declaration, you use <class T> or <class T,char S> or whatever
  32. have you, it doesn't matter.
  33.  
  34. : And then use it like that :
  35.  
  36. :   Func<1>();
  37. :   Func<2>();
  38. :   Func<3>();
  39.  
  40. : The compiler would generate three similar Func functions with T replaced 
  41. : by the values 1,2,3.
  42.  
  43. : If you know the syntax for such a template, PLEASE LET ME KNOW!!! :)
  44.  
  45. : If you're wondering why I'm looking for such a complicated thing, here's 
  46. : why :
  47.  
  48.     Quite the contrary, there is nothing complicated about this. Use
  49. somthing like this...
  50.  
  51. template <class T> 
  52. incline void Func(const T) {
  53.     cout << T << endl;
  54. }
  55.  
  56. : I have a function that uses a DEFINEd constant (which it absolutely 
  57. : needs for optimization reasons) and I want to include this function in a
  58.  
  59.     Let me get this right. You want to use only a constant value but you
  60. want to use templates also? This is a contradiction. The whole point of
  61. templating is that you do not know while writing it what type of data will
  62. be passed to it, so if in this case, you will ALWAYS know what is passed,
  63. why template it at all? My above example should be placed into a header and
  64. NOT compiled into a lib or it won't work properly. THat way, when you are
  65. coding, you include the header, write your code using the Func call and your
  66. compiler will instantiate the template. 
  67.  
  68. : library for all my demo group other coders to use.  The problem is that, 
  69. : if the function is used 4 times in the program, it'll be with 4 
  70. : different values assigned to the constant...
  71.  
  72.     You really are missing the point behind templates... consider the
  73. following...
  74.  
  75. template <class T> 
  76. const T& Max(const T& A,const T& B) { 
  77.     return (A >= B?A:B);
  78. }
  79.     Here you are returning data of type T by comparing two other data
  80. types of T. If these were longs, ints, doubles, floats , you would achieve
  81. the desired results. If you were to then code something like this, you would
  82. get an error....
  83.  
  84.     double a = Max(const int a, const float b);
  85.  
  86.     Big time error, the data types have to be the same as each other.
  87. The "T" in the above case is all in the same scope and is consider to all be
  88. one type of data that is the same.
  89.  
  90. : If you have any idea else than the "hypothetical" template <const T>, 
  91. : please let me know...
  92.  
  93.     Well, this should help you get started. I hope you buy a book on
  94. this, it sounds like you have a ways to go yet with templates.
  95.     
  96. Take care!
  97.  
  98. --
  99. Steve
  100.